<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="转是转了，但是不是感觉有点不太对劲呢？太慢了？别急，咱们继续。根据本章开头讲解的原理，八拍模式时，步进电机转过一圈是需要 64 个节拍，而我们程序中是每个节拍持续 2ms，那么转一圈就应该是 128ms，即 1 秒钟转 7 圈多，可怎么看上去它好像是 7 秒多才转了一圈呢？ 那么，是时候来了解“永磁式减速步进电机”中这个“减速”的概念了。图 9-7 是这个28BYJ-48 步进电机的拆解图，从图中">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="9.6、转动精度与深入分析">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu061/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="转是转了，但是不是感觉有点不太对劲呢？太慢了？别急，咱们继续。根据本章开头讲解的原理，八拍模式时，步进电机转过一圈是需要 64 个节拍，而我们程序中是每个节拍持续 2ms，那么转一圈就应该是 128ms，即 1 秒钟转 7 圈多，可怎么看上去它好像是 7 秒多才转了一圈呢？ 那么，是时候来了解“永磁式减速步进电机”中这个“减速”的概念了。图 9-7 是这个28BYJ-48 步进电机的拆解图，从图中">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q05514a3.png">
<meta property="og:updated_time" content="2019-01-27T04:52:49.207Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="9.6、转动精度与深入分析">
<meta name="twitter:description" content="转是转了，但是不是感觉有点不太对劲呢？太慢了？别急，咱们继续。根据本章开头讲解的原理，八拍模式时，步进电机转过一圈是需要 64 个节拍，而我们程序中是每个节拍持续 2ms，那么转一圈就应该是 128ms，即 1 秒钟转 7 圈多，可怎么看上去它好像是 7 秒多才转了一圈呢？ 那么，是时候来了解“永磁式减速步进电机”中这个“减速”的概念了。图 9-7 是这个28BYJ-48 步进电机的拆解图，从图中">
<meta name="twitter:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q05514a3.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu061/">





  <title>9.6、转动精度与深入分析 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu061/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">9.6、转动精度与深入分析</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:36:44+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>转是转了，但是不是感觉有点不太对劲呢？太慢了？别急，咱们继续。根据本章开头讲解的原理，八拍模式时，步进电机转过一圈是需要 64 个节拍，而我们程序中是每个节拍持续 2ms，那么转一圈就应该是 128ms，即 1 秒钟转 7 圈多，可怎么看上去它好像是 7 秒多才转了一圈呢？</p>
<p>那么，是时候来了解“永磁式减速步进电机”中这个“减速”的概念了。图 9-7 是这个28BYJ-48 步进电机的拆解图，从图中可以看到，位于最中心的那个白色小齿轮才是步进电机的转子输出，64 个节拍只是让这个小齿轮转了一圈，然后它带动那个浅蓝色的大齿轮，这就是一级减速。大家看一下右上方的白色齿轮的结构，除电机转子和最终输出轴外的 3 个传动齿轮都是这样的结构，由一层多齿和一层少齿构成，而每一个齿轮都用自己的少齿层去驱动下一个齿轮的多齿层，这样每 2 个齿轮都构成一级减速，一共就有了 4 级减速，那么总的减速比是多少呢？即转子要转多少圈最终输出轴才转一圈呢？</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q05514a3.png" alt></p>
<center><strong>图 9-7  步进电机内部齿轮示意图</strong></center>

<p>回头看一下电机参数表中的减速比这个参数吧——1:64，转子转 64 圈，最终输出轴才会转一圈，也就是需要 64<em>64=4096 个节拍输出轴才转过一圈，2ms</em>4096=8192ms，8 秒多才转一圈呢，是不是跟刚才的实验结果正好吻合了？4096 个节拍转动一圈，那么一个节拍转动的角度——步进角度就是 360/4096，看一下表中的步进角度参数 5.625/64，算一下就知道这两个值是相等的，一切都已吻合了。</p>
<p>关于基本的控制原理本该到这里就全部结束了，但是，我们希望大家都能培养一种“实践是检验真理的唯一标准”的思维方式！回想一下，步进电机最大的特点是什么？精确控制转动量！那么我们是不是应该检验一下它到底是不是能精确呢？精确到什么程度呢？怎么来检验呢？让它转过 90 度，然后量一下准不准？也行，但是如果它只差了 1 度甚至不到 1 度，你能准确测量出来吗？在没有精密仪器的情况很难。我们还是让它多转几个整圈，看看它最后停下的位置还是不是原来的位置。对应的，我们把程序修改一下，以方便控制电机转过任意的圈数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">void TurnMotor(unsigned long angle);</span><br><span class="line">void main()&#123;</span><br><span class="line">    TurnMotor(360*25);  //360 度*25，即 25 圈</span><br><span class="line">    while (1);</span><br><span class="line">&#125;</span><br><span class="line">/* 软件延时函数，延时约 2ms */</span><br><span class="line">void delay()&#123;</span><br><span class="line">    unsigned int i = 200;</span><br><span class="line">    while (i--);</span><br><span class="line">&#125;</span><br><span class="line">/* 步进电机转动函数，angle-需转过的角度 */</span><br><span class="line">void TurnMotor(unsigned long angle)&#123;</span><br><span class="line">    unsigned char tmp;  //临时变量</span><br><span class="line">    unsigned char index = 0;  //节拍输出索引</span><br><span class="line">    unsigned long beats = 0;  //所需节拍总数</span><br><span class="line">    //步进电机节拍对应的 IO 控制代码</span><br><span class="line">    unsigned char code BeatCode[8] = &#123;</span><br><span class="line">        0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6</span><br><span class="line">    &#125;;</span><br><span class="line">    //计算需要的节拍总数，4096 拍对应一圈</span><br><span class="line">    beats = (angle*4096) / 360;</span><br><span class="line">    //判断 beats 不为 0 时执行循环，然后自减 1</span><br><span class="line">    while (beats--)&#123;</span><br><span class="line">        tmp = P1;  //用 tmp 把 P1 口当前值暂存</span><br><span class="line">        tmp = tmp &amp; 0xF0;  //用&amp;操作清零低 4 位</span><br><span class="line">        tmp = tmp | BeatCode[index]; //用|操作把节拍代码写到低 4 位</span><br><span class="line">        P1 = tmp;  //把低 4 位的节拍代码和高 4 位的原值送回 P1</span><br><span class="line">        index++;  //节拍输出索引递增</span><br><span class="line">        index = index &amp; 0x07;  //用&amp;操作实现到 8 归零</span><br><span class="line">        delay();  //延时 2ms，即 2ms 执行一拍</span><br><span class="line">    &#125;</span><br><span class="line">    P1 = P1 | 0x0F; //关闭电机所有的相</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述程序中，我们先编写了一个控制电机转过指定角度的函数，这个角度值由函数的形式参数给出，然后在主函数中就可以方便的通过更改调用时的实际参数来控制电机转过任意的角度了。我们用了 360<em>25，也就是 25 圈，当然你也可以随意改为其它的值，看看是什么结果。我们的程序会执行 25</em>8=200 秒的时间，先记下输出轴的初始位置，然后上电并耐心等它执行完毕，看一下，是不是„„有误差？怎么回事，哪儿出问题了，不是说能精确控制转动量吗？</p>
<p>这个问题其实是出在了减速比上，再来看一下，厂家给出的减速比是 1:64，不管是哪个厂家生产的电机，只要型号是 28BYJ-48，其标称的减速比就都是 1:64。但实际上呢？经过我们的拆解计算发现：真实准确的减速比并不是这个值 1:64，而是 1:63.684！得出这个数据的方法也很简单，实际数一下每个齿轮的齿数，然后将各级减速比相乘，就可以得出结果了，实测的减速比为(32/9)<em>(22/11)</em>(26/9)<em>(31/10)≈63.684，从而得出实际误差为 0.0049，即约为百分之 0.5，转 100 圈就会差出半圈，那么我们刚才转了 25 圈，是不是就差了八分之一圈了，也就是 45 度，看一下刚才的误差是 45 度吧。那么按照 1:63.684 的实际减速比，可以得出转过一圈所需要节拍数是 64</em>63.684≈4076。那么就把上面程序中电机驱动函数里的 4096 改成4076 再试一下吧。是不是看不出丝毫的误差了？但实际上误差还是存在的，因为上面的计算结果都是约等得出的，实际误差大约是 0.000056，即万分之 0.56，转一万圈才会差出半圈，已经可以忽略不计了。</p>
<p>那么厂家的参数为什么会有误差呢？难道厂家不知道吗？要解释这个问题，我们得回到实际应用中，步进电机最通常的目的是控制目标转过一定的角度，通常都是在 360 度以内的，而这个 28BYJ-48 最初的设计目的是用来控制空调的扇叶的，扇叶的活动范围是不会超过 180度的，所以在这种应用场合下，厂商给出一个近似的整数减速比 1:64 已经足够精确了，这也是合情合理的。然而，正如我们的程序那样，我们不一定是要用它来驱动空调扇叶，我们可以让它转动很多圈来干别的，这个时候就需要更为精确的数据了，这也是我们希望同学们都能了解并掌握的，就是说我们要能自己“设计”系统并解决其中发现的问题，而不要被所谓的“现成的方案”限制住思路。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu060/" rel="next" title="9.5、让电机转起来">
                <i class="fa fa-chevron-left"></i> 9.5、让电机转起来
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu062/" rel="prev" title="9.7、电机控制程序基础">
                9.7、电机控制程序基础 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
